<template>
    <div class="login">
        <div class="loginForm">
          <div class="title">数据共享系统</div>
          <a-form ref="formRef" :model="formState" class="form">
            <a-form-item name="username" :rules="[{ required: true, message: '请输入用户名' }]">
              <a-input v-model:value="formState.username" placeholder="admin">
                <template #prefix>
                  <user-outlined type="user" />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item name="password" :rules="[{ required: true, message: '请输入密码' }]" style="margin: 5px 0;">
              <a-input v-model:value="formState.password" placeholder="123456">
                <template #prefix>
                  <unlock-outlined type="user" />`
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="" style="text-align: center;margin-top: 30px;">
              <a-button @click="login" type="primary">登录</a-button>
              <a-button @click="reset" style="margin-left:20px;">重置</a-button>
            </a-form-item>
          </a-form>
        </div>
    </div>
</template>

<script setup lang="ts">
import { message } from 'ant-design-vue';
import { useStore } from 'vuex'
import { useRouter } from 'vue-router';
import { computed, reactive, ref, watch } from 'vue';
import { UserOutlined, UnlockOutlined } from '@ant-design/icons-vue';
  const state = reactive({
    store: useStore(),
    router: useRouter()
  })
  const formRef = ref()
  const formState = reactive({
    username: 'admin',
    password: '123456',
  })
  const reset = () => {
    formRef.value.resetFields()
  }
  const login = () => {
    formRef.value.validateFields().then((res: any) => {
      console.log(res)
      if(res.username == 'admin' && res.password == '123456') {
        message.success('登录成功')
        let users:Object = {
          username:res.username,
          password:res.password,
          token:new Date(),
        }
        state.store.commit('setUser',users)
        localStorage.setItem('user',JSON.stringify(users))
        state.router.push({
          path: '/welcome'
        })
      } else {
        message.error('用户名或密码错误')
      }
    }).catch((err: any) => {
      console.log(err)
    })
  }
</script>

<style lang="scss" scoped>
@function cvw($px) {
  @return $px/1920 * 100vw;
}
.login{
  width: cvw(1920);
  height: 100%;
  background-image: url('/src/assets/bg4.png');
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .loginForm{
    margin-left: -100px;
    width: cvw(560);
    height: cvw(500);
    background: rgba(255,255,255,0.88);
    padding: cvw(100) cvw(40);
  }
  .title{
    font-size: cvw(40);
    font-weight: bold;
    text-align: center;
    margin-bottom: cvw(50);
  }
  .form{
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}
</style>
